﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WIA;
namespace TwainTest
{
    class ScanInterface
    {
        public static int ScanAndSaveTo(string path) 
        {
            try
            {
                DeviceManager manager = new DeviceManagerClass();
                Device device = null;
                foreach (DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.Type != WiaDeviceType.ScannerDeviceType)
                        continue;
                    device = info.Connect();
                    break;
                }
                Item item = device.Items[1];
                CommonDialogClass cdc = new WIA.CommonDialogClass();
                ImageFile imageFile = cdc.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType,
                                                WIA.WiaImageIntent.TextIntent,
                                                WIA.WiaImageBias.MaximizeQuality, "{00000000-0000-0000-0000-000000000000}", true, true, false);
                if (imageFile != null)
                {
                    imageFile.SaveFile(path);
                    return 1;
                }
                else
                    return 0; //no valid image
                //imageFile = cdc.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType,
                //                                WIA.WiaImageIntent.TextIntent,
                //                                WIA.WiaImageBias.MaximizeQuality, "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}", true, true, false);
                
            }
            catch
            {
                return -1;//no device error
            }
        }
    }
}
